<?php
/**
 * Building Model for RoomBooking Component
 *
 * @version    1.0
 * @package    Joomla
 * @subpackage RoomBooking
 * @copyright (C) 2011 Giovanni Marzulli
 * @license	   GNU/GPL
 */

// No direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport('joomla.application.component.model');

class RoomBookingModelBuilding extends JModel
{
	/**
	 * Constructor that retrieves the ID from the request
	 *
	 * @access	public
	 * @return	void
	 */
	function __construct()
	{
		parent::__construct();

		$array = JRequest::getVar('cid',  0, '', 'array');
		$this->setId((int)$array[0]);
	}

	/**
	 * Method to set the identifier
	 *
	 * @access	public
	 * @param	int identifier
	 * @return	void
	 */
	function setId($id)
	{
		// Set id and wipe data
		$this->_id		= $id;
		$this->_data	= null;
	}
	
	/**
	 * Method to get organizations of building
	 * @return object with data
	 */
	function getOrgsAssociate()
	{
		// Load the data
		$query = ' SELECT id_organization AS id FROM #__roombooking_building_organization WHERE id_building = '.$this->_id;
		$orgs_associate = $this->_getList( $query );

		return $orgs_associate;
	}
	
	/**
	 * Method to get a building record
	 * @return object with data
	 */
	function &getData()
	{
		// Load the data
		if (empty( $this->_data )) {
			$query = ' SELECT * FROM #__roombooking_buildings WHERE id = '.$this->_id;
			$this->_db->setQuery( $query );
			$this->_data = $this->_db->loadObject();
		}
		if (!$this->_data) {
			$this->_data = new stdClass();
			$this->_data->id = 0;
			$this->_data->name = null;
			$this->_data->address = null;
			$this->_data->opening = null;
			$this->_data->closing = null;
			$this->_data->we_opening = null;
		}
		return $this->_data;
	}

	/**
	 * Method to store a record
	 *
	 * @access	public
	 * @return	boolean	True on success
	 */
	function store()
	{	
		$row =& $this->getTable();
		
		$data = JRequest::get( 'post' );

		// Bind the form fields to the building table
		if (!$row->bind($data)) {
			$this->setError($this->_db->getErrorMsg());
			return false;
		}

		// Make sure the building record is valid
		if (!$row->check()) {
			$this->setError($this->_db->getErrorMsg());
			return false;
		}

		// Set the error message
		if (!$row->store()) {
			$this->setError( $row->getErrorMsg() );
			return false;
		}
		return $row->id;
	}
	
	
	/**
	 * Method to set organizations-building associations
	 * @return boolean	True on success
	 */
	function storeBuildOrg($id_build){
		$orgs = JRequest::getVar('orgs');
		$query = ' DELETE FROM #__roombooking_building_organization '. 
				 ' WHERE id_building = '.$id_build;
		$this->_db->setQuery( $query );
		$this->_db->query();
		
		foreach ($orgs as $org){
			$data = new stdClass();
			$data->id_building = $id_build;
			$data->id_organization = $org;
			if ($this->_db->insertObject( '#__roombooking_building_organization', $data ))
				;
			else 
				return false;
		}
		return true;
	}
	
	/**
	 * Method to delete organizations-building associations
	 * @return boolean	True on success
	 */
	function deleteBuildOrg($cids){
		if (count( $cids )) {
			foreach($cids as $cid) {
				$query = ' DELETE FROM #__roombooking_building_organization '. 
				 		 ' WHERE id_building = '.$cid;
				$this->_db->setQuery( $query );
				$this->_db->query();
			}
		}
	}
	
	
	/**
	 * Method to delete record(s)
	 *
	 * @access	public
	 * @return	boolean	True on success
	 */
	function delete()
	{
		JLoader::import( 'room', JPATH_COMPONENT_ADMINISTRATOR . DS . 'models' );
		$modelRoom = JModel::getInstance( 'room', 'RoomBookingModel' );
		
		$cids = JRequest::getVar( 'cid', array(0), 'post', 'array' );

		$row =& $this->getTable();

		if (count( $cids )) {
			foreach($cids as $cid) {
				if (!$row->delete( $cid )) {
					$this->setError( $row->getErrorMsg() );
					return false;
				}
				$modelRoom->roomCidsOfBuilding($cid);
			}
		}
		$this->deleteBuildOrg($cids);
		return true;
	}
	
	/**
	 * Method to get building opening and closing time
	 * @return object with data
	 */
	function getBuildOpenCloseTime($id){
		$query = 'SELECT date_format( opening, "%H:%i" ) as opening, date_format( closing, "%H:%i" ) as closing  FROM #__roombooking_buildings WHERE id = '.$id;
		$this->_db->setQuery( $query );
		$this->_db->query();
		$times = $this->_db->loadObject();
		return $times;	
		
	}
	
	/**
	 * Method to get weekend opening
	 * @return boolean
	 */	
	function getWEOpening($id){
		$query = 'SELECT we_opening  FROM #__roombooking_buildings WHERE id = '.$id;
		$this->_db->setQuery( $query );
		$this->_db->query();
		$we_opening = $this->_db->loadObject();
				
		return $we_opening->we_opening;	
		
    }
    
    /**
	 * Method to get building name
	 * @return string
	 */
	function getBuildName($id_building)
    {
		$db =& JFactory::getDBO();
 
  		$query = 'SELECT  name FROM #__roombooking_buildings WHERE id='.$id_building;
		$db->setQuery( $query );
   		$build = $db->loadObject();
   		
   		return $build->name;
    }

}